package com.ag.lzw.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.bean.TbMedicineConfirm;
import com.ag.bean.TbTask;
import com.ag.lzw.common.TaskTreeManager;
import com.ag.lzw.dao.LzwTaskDAO;
import com.ag.lzw.dao.LzwTbMedicineConfirmDAO;
import com.ag.lzw.service.MedicineConfirm;
import com.ag.lzw.vo.TaskTreeVo;
@Service("MedicineConfirm")
public class MedicineConfirmImpl implements MedicineConfirm {

	private static final Logger logger = Logger.getLogger(MedicineConfirmImpl.class);
	
	@Resource(name="lzwTbMedicineConfirmDAO")
	private LzwTbMedicineConfirmDAO mcDAO;
	@Resource(name="taskTreeManager")
	private TaskTreeManager taskTree;	
	@Resource(name = "lzwTaskDao")
	private LzwTaskDAO taskDAO;

	@Override
	public TbMedicineConfirm queryMedicineConfirm(String tid) {
		List<TbMedicineConfirm> list = mcDAO.findMC(tid);
			if(list!=null && list.size()>0){
				return list.get(0);
			} else {
				return null;
			}
	}

	@Override
	public boolean saveMedicineConfirm(String tid, String cnum, String mnum,
			String dnum, String wnum) {
		TbMedicineConfirm  tmc = this.mcDAO.findById(tid);
		tmc.setMcCnum(cnum);
		tmc.setMcDnum(dnum);
		tmc.setMcMnum(mnum);
		tmc.setMcWnum(wnum);
		tmc.setMcTime(new Date());
		this.mcDAO.attachDirty(tmc);
		return false;
	}

	@Override
	public List<TbMedicineConfirm> queryChildMedicineConfirm(String tid) {
		List<TbMedicineConfirm> tmcs = new ArrayList<TbMedicineConfirm>(); 
		Set<TbTask> taskSet = this.taskTree.getChildTask(tid);
		logger.info("foreach the tasktree!");
		if(taskSet!=null && taskSet.size()>0){
			Iterator<TbTask> it = taskSet.iterator();
			while(it.hasNext()){
				TbTask tt = it.next();
				TbMedicineConfirm tmc = queryMedicineConfirm(tt.getTId());
				tmcs.add(tmc);
			}
		}
		logger.info("foreach tasktree over!");
		return tmcs;
	}
	
	/**
	 * 
	 */
	@Override
	public List queryLeaderMC(String uid) {
		List<TbMedicineConfirm> tmcs = new ArrayList<TbMedicineConfirm>(); 
		List<TbTask> list = this.taskDAO.findTasksByB(uid);
		for(TbTask tt : list){
			TbMedicineConfirm tmc = queryMedicineConfirm(tt.getTId());
			tmcs.add(tmc);
		}
		return tmcs;
	}
}
